package cn.darkhorse.workorder.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.darkhorse.workorder.domain.QmsWorkorderHvi;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.darkhorse.workorder.mapper.QmsWorkorderHviRecordMapper;
import cn.darkhorse.workorder.domain.QmsWorkorderHviRecord;
import cn.darkhorse.workorder.service.IQmsWorkorderHviRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * hvi检验记录
Service业务层处理
 *
 * @author ytq
 * @date 2024-07-19
 */
@Service
public class QmsWorkorderHviRecordServiceImpl extends ServiceImpl<QmsWorkorderHviRecordMapper, QmsWorkorderHviRecord> implements IQmsWorkorderHviRecordService
{
    @Autowired
    private QmsWorkorderHviRecordMapper qmsWorkorderHviRecordMapper;



    @Override
    public List<QmsWorkorderHviRecord> selectByHvi(QmsWorkorderHvi hvi) {

        return lambdaQuery()
                .eq(QmsWorkorderHviRecord::getHviId, hvi.getId())
                .or()
                .eq(QmsWorkorderHviRecord::getOrderNumber, hvi.getOrderNumber())
                .list();
    }





    /**
     * 保存hvi检验记录
     * @param workorderHviRecords
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<QmsWorkorderHviRecord> saveHviRecord(List<QmsWorkorderHviRecord> workorderHviRecords) {
        for (QmsWorkorderHviRecord record : workorderHviRecords) {
            List<QmsWorkorderHviRecord> list = lambdaQuery()
                    .eq(QmsWorkorderHviRecord::getOrderNumber, record.getOrderNumber())
                    .eq(QmsWorkorderHviRecord::getHviMachineNumber, record.getHviMachineNumber()).list();
            if (list.isEmpty()){
                save(record);
            }
        }
        String orderNumber = workorderHviRecords.get(0).getOrderNumber();
        return lambdaQuery().eq(QmsWorkorderHviRecord::getOrderNumber,orderNumber).list();
    }
}
